import 'package:cloud_medicine/components/author_show.dart';
import 'package:cloud_medicine/components/util.dart';
import 'package:cloud_medicine/main.dart';
import 'package:cloud_medicine/model/user_verify_list.dart';
import 'package:cloud_medicine/pages/user_verify_detail.dart';
import 'package:flutter/material.dart';

import '../components/util.dart';
import '../http/user_api.dart';

class BackPage extends StatefulWidget {
  @override
  _BackPageState createState() => _BackPageState();
}

class _BackPageState extends State<BackPage> {
  bool _loading = false;
  List<UserVerifyItem> _list = [];

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  // 加载数据
  Future<Null> _loadData() async {
    setState(() {
      _loading = true;
    });
    dynamic res = await getUserVerifyList_api();
    if (res['code'] == 0) {
      res['data'].forEach((dynamic userVerifyItem) {
        if (mounted) {
          setState(() {
            UserVerifyItem userVerifyItemModel =
                UserVerifyItem.init(userVerifyItem);
            _list.add(userVerifyItemModel);
          });
        }
      });
    } else {
      errorToast(res['message']);
    }
    if (mounted) {
      setState(() {
        _loading = false;
      });
    }
  }

  // 进入验证材料详情
  _entryVerifyDetail(int id) {
    Navigator.of(context).push(
      new MaterialPageRoute(
        builder: (context) => UserVerifyDetailPage(id: id),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          centerTitle: true,
          title: Text('后台管理'),
          automaticallyImplyLeading: false,
          leading: Tooltip(
            message: '返回前台',
            child: IconButton(
              icon: Icon(Icons.home),
              onPressed: () {
                Navigator.of(context).push(
                  new MaterialPageRoute(
                    builder: (context) => MyHomePage(initIndex: 4),
                  ),
                );
              },
            ),
          )),
      body: _loading
          ? loadingWidget(_loading)
          : ListView.builder(
              itemCount: _list.length,
              itemBuilder: (BuildContext context, int index) {
                bool hasPicture = _list[index].firstPicture != null;
                return InkWell(
                  onTap: () {
                    _entryVerifyDetail(_list[index].id);
                  },
                  child: Container(
                    padding: EdgeInsets.all(10),
                    child: ListView(
                      shrinkWrap: true,
                      physics: new NeverScrollableScrollPhysics(),
                      children: <Widget>[
                        AuthorShow(
                          userID: _list[index].authorBasicInfo.id,
                          realname: _list[index].authorBasicInfo.realname,
                          avatar: _list[index].authorBasicInfo.avatar,
                          createTime: _list[index].createTime,
                        ),
                        SizedBox(height: hasPicture ? 0 : 10),
                        Row(
                          children: <Widget>[
                            Expanded(
                              child: Text(
                                _list[index].abstract,
                                maxLines: 3,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(color: Colors.grey),
                              ),
                            ),
                            SizedBox(width: 10),
                            hasPicture
                                ? Container(
                                    width: 100,
                                    height: 100,
                                    child: Image.network(
                                        _list[index].firstPicture),
                                  )
                                : Container(),
                          ],
                        ),
                        SizedBox(height: hasPicture ? 0 : 10),
                        Divider(
                          height: 1,
                        )
                      ],
                    ),
                  ),
                );
              },
            ),
    );
  }
}
